
package xib.android;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import java.util.ArrayList;
import xib.Page;
import xib.Panel;
import xib.core.PanelXib;
import xib.core.SpeechBubbleXib;
import xib.parserTools.interfaces.ISpeechBubbleData.SpeechBubbleType;

/**
 *
 * @author xavierds
 */
final class BitmapBuilder {


    /**
     * Obtain a bitmap with the panel image loaded.
     * @param panel
     * @return
     */
    static android.graphics.Bitmap getPanel(String language, Panel panel){
        PanelXib panelXib = (PanelXib) panel;
        SpeechBubbleXib speech;

        //Creamos un bitmap del tamanyo del panel
        Bitmap b = Bitmap.createBitmap(panelXib.getWidth(), panelXib.getHeight(), Config.RGB_565);
        Bitmap aux;

        //Sacamos los distintos speechBubbles
        ArrayList<SpeechBubbleXib> list = panelXib.getSpeechBubblesXib(language);
        
        int height, width, pixels[];

        height = panelXib.getHeight();
        width = panelXib.getWidth();
        pixels = new int[height * width];
        aux = BitmapFactory.decodeStream(panelXib.getInputStream());
        aux.getPixels(pixels, 0, width, 0, 0, width, height);
        b.setPixels(pixels, 0, width, 0, 0, width, height);

        if(list != null)
        for(int i=0; i < list.size(); i++){
            speech = list.get(i);
            if(SpeechBubbleType.OtherLanguage == speech.getSpeechBubbleType()){
                height = speech.getHeight();
                width = speech.getWidth();
                pixels = new int[height * width];
                aux = BitmapFactory.decodeStream(speech.getInputStream());
                aux.getPixels(pixels, 0, width, 0, 0, width, height);
                b.setPixels(pixels, 0, width, speech.getLocationX(),
                    speech.getLocationY(), width, height);
            }
        }
        return b;
        //return BitmapFactory.decodeStream(((PanelXib) panel).getInputStream());
    }


    /**
     * Obtain a bitmap with the page image loaded. For this is creating an
     * empty bitmap from the size of the page and then loading inside all
     * panels one by one.
     * @param page
     * @return
     */
    static android.graphics.Bitmap getPage(String language, Page page){
        Bitmap b = Bitmap.createBitmap(page.getWidth(), page.getHeight(),
                Config.RGB_565);
        Bitmap aux;
        PanelXib panel;
        int height, width, pixels[];
        int size = page.getQuantityOfPanels();
        for(int i = 1; i <= size; i++){
            panel = (PanelXib) page.getPanel(i);
            //aux = BitmapFactory.decodeStream(panel.getInputStream());
            aux = getPanel(language, panel);
            height = panel.getHeight();
            width = panel.getWidth();
            pixels = new int[height * width];
            aux.getPixels(pixels, 0, width, 0, 0, width, height);
            b.setPixels(pixels, 0, width, panel.getLocationX(),
                    panel.getLocationY(), width, height);
        }
        return b;
    }
}
